/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.web.action;

import com.wii.elpro.dao.ProdukDAOInterface;
import com.wii.elpro.dao.hb.PriceDAOImpl;
import com.wii.elpro.dao.hb.ProdukDAOImpl;
import com.wii.elpro.entity.Produk;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.ProductUpdateForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Vyor
 */
public class ProductUpdateAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        ProductUpdateForm produkUpdateForm = (ProductUpdateForm) form;
        ProdukDAOInterface dao = new ProdukDAOImpl();
        Produk product = new Produk();
        
        product.setId(produkUpdateForm.getId());
        product.setKode(produkUpdateForm.getKode());
        product.setNama(produkUpdateForm.getNama());
        product.setWeight(produkUpdateForm.getWeight());
        product.setStockLI(produkUpdateForm.getStockLI());
        product.setStockLE(produkUpdateForm.getStockLE());
        product.setHargaHpp(new PriceDAOImpl().getPrice(Long.parseLong(request.getParameter("idHargaHpp"))));
        
        ActionMessages messages = new ActionMessages();        
        try {           
            dao.update(product);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully updated."));
            saveMessages(sess, messages);
            forward = "success";
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to update your data. Please contact my developer."));
            saveMessages(sess, messages);
            forward = "fail";
            ex.printStackTrace();
        }
        return mapping.findForward(forward);
    }
}
